<template>
  <el-dialog
    :title="!dataForm.deptId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="父部门id" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="父部门id"></el-input>
    </el-form-item>
      <el-form-item label="上级部门" prop="parentName">
        <el-popover
          ref="menuListPopover"
          placement="bottom-start"
          trigger="click">
          <el-tree
            :data="menuList"
            :props="menuListTreeProps"
            node-key="deptId"
            ref="menuListTree"
            @current-change="menuListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input v-model="dataForm.parentName" v-popover:menuListPopover :readonly="true" placeholder="点击选择上级部门" class="menu-list__input"></el-input>
      </el-form-item>

    <el-form-item label="部门名称" prop="deptName">
      <el-input v-model="dataForm.deptName" placeholder="部门名称"></el-input>
    </el-form-item>
    <el-form-item label="显示顺序" prop="orderNum">
      <el-input v-model="dataForm.orderNum" placeholder="显示顺序"></el-input>
    </el-form-item>
    <el-form-item label="部门状态" prop="status">
      <el-select v-model="dataForm.status" placeholder="部门状态">
        <el-option key="0" label="正常" value="0"></el-option>
        <el-option key="1" label="停用" value="1"></el-option>
      </el-select>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          deptId: 0,
          parentId: '',
          deptName: '',
          orderNum: '',
          status: '',
          parentName: ''
        },
        dataRule: {
          parentId: [
            { required: true, message: '父部门id不能为空', trigger: 'blur' }
          ],
          parentName: [
            { required: true, message: '父部门名称不能为空', trigger: 'blur' }
          ],
          deptName: [
            { required: true, message: '部门名称不能为空', trigger: 'blur' }
          ],
          orderNum: [
            { required: true, message: '显示顺序不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '部门状态（0正常1停用）不能为空', trigger: 'blur' }
          ]
        },
        menuList: [],
        menuListTreeProps: {
          label: 'deptName',
          children: 'children'
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.deptId = id || 0
        this.visible = true
        this.$http({
          url: this.$http.adornUrl('/PCguanli/linedept/list'),
          method: 'get',
          params: this.$http.adornParams({
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          this.menuList = treeDataTranslate(data, 'deptId')
        }).then(() => {
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            if (this.dataForm.deptId) {
              this.$http({
                url: this.$http.adornUrl(`/PCguanli/linedept/info/${this.dataForm.deptId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm.parentId = data.lineDept.parentId
                  this.dataForm.deptName = data.lineDept.deptName
                  this.dataForm.orderNum = data.lineDept.orderNum
                  this.dataForm.status = data.lineDept.status
                  this.menuListTreeSetCurrentNode()
                }
              })
            }
          })
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/PCguanli/linedept/${!this.dataForm.deptId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'deptId': this.dataForm.deptId || undefined,
                'parentId': this.dataForm.parentId,
                'deptName': this.dataForm.deptName,
                'orderNum': this.dataForm.orderNum,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 菜单树选中
      menuListTreeCurrentChangeHandle (data, node) {
        this.dataForm.parentId = data.deptId
        this.dataForm.parentName = data.deptName
      },
      // 菜单树设置当前选中节点
      menuListTreeSetCurrentNode () {
        this.$refs.menuListTree.setCurrentKey(this.dataForm.parentId)
        this.dataForm.parentName = (this.$refs.menuListTree.getCurrentNode() || {})['deptName']
      }
    }
  }
</script>
